package com.auxgroup.hr.front.service.personnal.impl;

import com.auxgroup.hr.front.commons.utils.DateUtils;
import com.auxgroup.hr.front.dao.domain.PsJob;
import com.auxgroup.hr.front.dao.domain.PsJobcodeTbl;
import com.auxgroup.hr.front.dao.repository.PsEthnicGrpTblRepository;
import com.auxgroup.hr.front.dao.repository.PsJobRepository;
import com.auxgroup.hr.front.dao.repository.PsJobcodeTblRepository;
import com.auxgroup.hr.front.service.personnal.JobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 附件记录
 * Created by nitongqian on 2019/1/18.
 */
@Service("jobService")
public class JobServiceImpl implements JobService {

    private static final Logger logger = LoggerFactory.getLogger(JobServiceImpl.class);

    @Autowired
    private PsJobRepository psJobRepository;

    @Autowired
    private PsJobcodeTblRepository psJobcodeTblRepository;

    @Override
    public List<PsJob> findAll(String emplid) {
        return psJobRepository.findByEmplid(emplid);
    }


    //首次入职时间逻辑
    @Override
    public Date getFirstInCompany(String emplid) {
        List<PsJob> jobs = psJobRepository.findJobs(emplid);
        if(jobs.isEmpty()) {
            return null;
        }
        Date earliestDate = DateUtils.parseDate("21000101", "YYYYMMDD");
        for(PsJob job : jobs) {
            if(job.getEffdt() != null && job.getEffdt().compareTo(earliestDate) < 0) {
                earliestDate = job.getEffdt();
            }
        }
        return earliestDate.equals(DateUtils.parseDate("21000101", "YYYYMMDD")) ? null : earliestDate;
    }

    @Override
    public String getPostShortNbr(String emplid) {
        List<PsJob> jobs = psJobRepository.findByEmplidAndEmplRcdOrderByEffdtDesc(emplid, BigDecimal.ZERO);
        if(jobs.isEmpty()) {
            return "";
        }

        List<PsJobcodeTbl> pjcodes = psJobcodeTblRepository.findByJobcodeOrderByEffdtDesc(jobs.get(0).getJobcode());

        return pjcodes.isEmpty() ? "" : pjcodes.get(0).getcPostShortNbr();
    }
}
